package com.zkj.controller.admin;


import com.zkj.constant.JwtClaimsConstant;
import com.zkj.constant.PasswordConstant;
import com.zkj.dto.EmployeeDTO;
import com.zkj.entity.Employee;
import com.zkj.mapper.EmployeeMapper;
import com.zkj.properties.JwtProperties;
import com.zkj.result.Result;
import com.zkj.constant.StatusConstant;
import com.zkj.service.IEmployeeService;
import com.zkj.utils.JwtUtil;
import com.zkj.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author zkj
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    @ApiOperation(value = "获取员工列表")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String name) {
        log.debug("pageNum: {}, pageSize: {}, name: {}", page, pageSize, name);
        return employeeService.page(page, pageSize, name);
    }

    @ApiOperation(value = "根据获id取员工信息")
    @GetMapping("{id}")
    public Result<Employee> getUserInfoById(@PathVariable Long id) {
        Employee employee = employeeMapper.selectById(id);
        return Result.success(employee);
    }

    @ApiOperation(value = "修改员工信息")
    @PutMapping()
    public Result<Employee> updateUserInfo(@RequestBody EmployeeDTO employeeDTO) {
        return employeeService.updateUserInfo(employeeDTO);
    }

    @PostMapping("/logout")
    @ApiOperation("员工退出登录")
    public Result<String> logout() {
        return Result.success();
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody EmployeeDTO employeeDTO) {
        log.info("进来了:登录 {}", employeeDTO);
        Employee employee = employeeService.login(employeeDTO);
        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String jwt = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), map);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder().id(employee.getId()).token(jwt).userName(employee.getName()).name(employee.getName()).build();

        return Result.success(employeeLoginVO);
    }

    @ApiOperation(value = "测试")
    @GetMapping("test")
    public String getTest() {
        log.info("执行了");
        return "111";
    }


    @ApiOperation(value = "添加员工")
    @PostMapping("add")
    public Result add(@RequestBody EmployeeDTO employeeDTO) {
        log.info("进来了:添加员工 {}", employeeDTO);
        return employeeService.addEmployee(employeeDTO);
    }

    @ApiOperation(value = "禁用启用员工")
    @PostMapping("status/{statusNum}")
    public Result setEmployeeStatus(Long id, @PathVariable Integer statusNum) {
        log.info("进来了:禁用启用员工 {}", id);
        return employeeService.setEmployeeStatus(id, statusNum);
    }
}
